package m202401.d07;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/7 09:25
 */
//82. 删除排序链表中的重复元素 II
public class Q82 {
    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(1);
        n1.next = n2;
        new Q82().deleteDuplicates(n1);
    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode res = new ListNode(-1);
        res.next = head;
        ListNode p = res;
        int val = Integer.MIN_VALUE;
        while (p.next != null && p.next.next != null) {
            if (val != p.next.val && p.next.val != p.next.next.val) {
                val = p.next.val;
                p = p.next;
                continue;
            }
            if (val == p.next.val) {
                p.next = p.next.next;
            } else {
                val = p.next.val;
            }
        }
        if (p.next != null && p.next.val == val) {
            p.next = null;
        }
        return res.next;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
